package com.sihai.ehr.service.impl;

import com.sihai.core.*;
import com.sihai.ehr.model.biz.FinancialInBillForm;
import com.sihai.ehr.model.biz.FinancialInBillFormAuditing;
import com.sihai.ehr.model.biz.FinancialInBillFormDetail;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.repository.AuditingRepository;
import com.sihai.ehr.service.FinancialInBillFormService;
import com.sihai.ehr.web.pojo.SearchFormPojo;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * author: wxn
 * since: 13-4-14 下午12:18
 */
@Service("financialInBillFormService")
public class FinancialInBillFormServiceImpl implements FinancialInBillFormService {
    @Resource
    private DataAccessor dataAccessor;
    @Resource
    private AuditingRepository auditingRepository;

    @Override
    public void save(FinancialInBillForm financialInBillForm, List<FinancialInBillFormDetail> details) throws Exception {
        Assert.notNull(financialInBillForm, "financialInBillForm is required");
        boolean isCreate = (financialInBillForm.getId() == null || financialInBillForm.getId() <= 0);
        dataAccessor.save(financialInBillForm);
        if(details != null){
            for(FinancialInBillFormDetail detail : details){
                detail.setBillForm(financialInBillForm);
                dataAccessor.save(detail);
            }
        }
        if(isCreate){
            saveFinancialInBillFormAuditing(financialInBillForm, 1);
        }
    }

    @Override
    public FinancialInBillForm get(Long id) throws Exception {
        Assert.notNull(id, "financialInBillForm id is required");
        return dataAccessor.get(FinancialInBillForm.class, id);
    }

    @Override
    public boolean delete(String ids) throws Exception {
        final String deleteSql = String.format("delete from FinancialInBillForm frt where frt.id in (%s)", ids);
        this.dataAccessor.batchUpdate(new QueryCreator() {
            @Override
            public Query createQuery(Session session) {
                return session.createQuery(deleteSql);
            }
        });
        return true;
    }

    @Override
    public LimitedList<FinancialInBillForm> findFinancialInBillForms(User user, final SearchFormPojo searchFormPojo, Limit limit, Sorter sorter) throws Exception {
        if(user == null || user.getWork() == null){
            if(searchFormPojo == null){
                return dataAccessor.findByHql(FinancialInBillForm.class, "from FinancialInBillForm", null, limit, sorter);
            }
            return dataAccessor.findByQuery(FinancialInBillForm.class, new QueryCreator(){
                @Override
                public Query createQuery(Session session) {
                    DynamicQuery query = new DynamicQuery("from FinancialInBillForm f where 1=1");
                    query.appendIfNotNull("and f.create_date > :startDate","startDate", searchFormPojo.getStartDate());
                    query.appendIfNotNull("and f.create_date < :endDate","endDate", searchFormPojo.getEndDate());
                    if(searchFormPojo.getOrgId() != null && searchFormPojo.getOrgId() > 0){
                        Organization o = dataAccessor.get(Organization.class, searchFormPojo.getOrgId());
                        if(o != null){
                            query.append("and f.work.organization.treePath like '" + o.getTreePath() + "%'");
                        }
                    }
                    query.appendIfNotNull("and f.work.duty.id = :dutyId", "dutyId", searchFormPojo.getDutyId());
                    return query.build(session);
                }
            }, limit, sorter);
        }
        return dataAccessor.findByHql(FinancialInBillForm.class, "from FinancialInBillForm f where f.work.id=?", new Object[]{user.getWork().getId()}, limit, sorter);
    }

    //查询财务报支单审核流程记录
    @Override
    public List<List<FinancialInBillFormAuditing>> findFinancialInBillFormAuditing(Long financialInBillFormId) throws Exception {
        return auditingRepository.findByHql(FinancialInBillFormAuditing.class, "from FinancialInBillFormAuditing ta where ta.financialInBillForm.id = ?", new Object[]{financialInBillFormId});
    }

    //保存财务报支单审核流程记录
    @Override
    public void saveFinancialInBillFormAuditingProcess(Long financialInBillFormId) {
        saveFinancialInBillFormAuditing(dataAccessor.get(FinancialInBillForm.class, financialInBillFormId), 1);
    }

    //创建审核流程
    private void saveFinancialInBillFormAuditing(FinancialInBillForm financialInBillForm, int auditNo) {
        auditingRepository.saveAuditingProcess(financialInBillForm, auditNo);
    }

    public List<FinancialInBillFormDetail> findFinancialInBillFormDetails(FinancialInBillForm financialInBillForm){
        Assert.notNull(financialInBillForm, "financialInBillForm is required");
        return dataAccessor.findByHql(FinancialInBillFormDetail.class, "from FinancialInBillFormDetail d where d.billForm.id=?", new Object[]{financialInBillForm.getId()}, null, null);
    }
}
